package com.huilian.iotbox.admin.wap.service.impl;

import com.huilian.iotbox.admin.wap.dao.DeviceDao;
import com.huilian.iotbox.admin.wap.dao.PushConfigStockDao;
import com.huilian.iotbox.admin.wap.dao.PushConfigStockItemDao;
import com.huilian.iotbox.admin.wap.service.PushConfigStockService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.DeviceDto;
import com.huilian.iotbox.data.dto.PushConfigStockDto;
import com.huilian.iotbox.data.enums.DeviceTypeEnum;
import com.huilian.iotbox.data.enums.PushConfigStockTypeEnum;
import com.huilian.iotbox.data.enums.PushConfigTypeEnum;
import com.huilian.iotbox.data.vo.CommodityProjectRelevanceVo;
import com.huilian.iotbox.data.vo.DeviceVo;
import com.huilian.iotbox.data.vo.PushConfigStockItemVo;
import com.huilian.iotbox.data.vo.PushConfigStockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LinYinDong
 * @date 2024/7/23 下午6:25
 */
@Service
public class PushConfigStockServiceImpl implements PushConfigStockService {
    @Autowired
    private DeviceDao deviceDao;
    @Autowired
    private PushConfigStockDao pushConfigStockDao;
    @Autowired
    private PushConfigStockItemDao pushConfigStockItemDao;
    @Override
    public ServerResponse findPushConfigStockDeviceNo(PushConfigStockDto pushConfigStockDto) {
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setUserInfo(pushConfigStockDto.getUserInfo());
        List<Integer> deviceTypeIdList = new ArrayList<>();
        deviceTypeIdList.add(DeviceTypeEnum.VENDING_MACHINE.getCode());
        deviceTypeIdList.add(DeviceTypeEnum.SCREEN_VENDING_MACHINE.getCode());
        deviceTypeIdList.add(DeviceTypeEnum.OPEN_DOOR_VENDING_MACHINE.getCode());
        deviceDto.setDeviceTypeIdList(deviceTypeIdList);
        final List<DeviceVo> deviceVoList = deviceDao.findDevice(deviceDto);
        List<PushConfigStockItemVo> stockVoList = pushConfigStockDao.findPushConfigStockByUserIdList(pushConfigStockDto);
        List<PushConfigStockItemVo> list = pushConfigStockDao.findPushConfigStockItemListByUserId(pushConfigStockDto);
        List<PushConfigStockItemVo> listOne = new ArrayList<>();
        List<PushConfigStockItemVo> listTwo = new ArrayList<>();
        List<PushConfigStockItemVo> listThree = new ArrayList<>();
        List<PushConfigStockItemVo> listDefault = new ArrayList<>();
        List<PushConfigStockItemVo> listAll = new ArrayList<>();
        for (DeviceVo deviceVo : deviceVoList) {
            boolean found = false;
            for (PushConfigStockItemVo pushConfigStockItemVo : list) {
                if (deviceVo.getId().equals(pushConfigStockItemVo.getDeviceId())) {
                    int type = pushConfigStockItemVo.getType();
                    if (type == PushConfigStockTypeEnum.COMPLETE_MACHINE_TYPE.getCode()) {
                        listOne.add(pushConfigStockItemVo);
                    } else if (type == PushConfigStockTypeEnum.COMPLETE_MACHINE.getCode()) {
                        listTwo.add(pushConfigStockItemVo);
                    } else if (type == PushConfigStockTypeEnum.FREIGHT_ROAD.getCode()) {
                        listThree.add(pushConfigStockItemVo);
                    }
                    listAll.add(pushConfigStockItemVo);
                    found = true;
                }
            }
            // 如果没有匹配到任何设备，添加默认项
            if (!found) {
                PushConfigStockItemVo defaultItem = new PushConfigStockItemVo();
                defaultItem.setDeviceId(deviceVo.getId());
                defaultItem.setStoreName(deviceVo.getStoreName());
                defaultItem.setDeviceNo(deviceVo.getDeviceNo());
                listDefault.add(defaultItem);
            }
        }
        Map<String,List<PushConfigStockItemVo>> map = new HashMap<>();
        map.put("listOne",listOne);
        map.put("listTwo",listTwo);
        map.put("listThree",listThree);
        map.put("listDefault",listDefault);
        map.put("listAll",listAll);
        map.put("stockList",stockVoList);
        return ServerResponse.createBySuccess(map);
    }

    @Transactional
    @Override
    public ServerResponse editPushConfigStock(PushConfigStockDto pushConfigStockDto) {
        if(StringUtils.isEmpty(pushConfigStockDto.getType())){
            return ServerResponse.createByErrorMessage("操作类别不能为空");
        }
        if(StringUtils.isEmpty(pushConfigStockDto.getWarnStock())){
            return ServerResponse.createByErrorMessage("库存通知数量不能为空");
        }
        if(pushConfigStockDto.getWarnStock() < 0){
            return ServerResponse.createByErrorMessage("库存通知数量不能为负数");
        }
        PushConfigStockVo pushConfigStockVo = pushConfigStockDao.getOnePushConfigStockByTypeAndUserId(pushConfigStockDto.getType(),pushConfigStockDto.getUserId());
        PushConfigStockDto pushConfigStockDto1 = new PushConfigStockDto();
        pushConfigStockDto1.setUserId(pushConfigStockDto.getUserId());
        pushConfigStockDto1.setType(pushConfigStockDto.getType());
        pushConfigStockDto1.setWarnStock(pushConfigStockDto.getWarnStock());
        List<PushConfigStockItemVo> oldList = pushConfigStockDao.findPushConfigStockItemListByUserId(pushConfigStockDto);
        List<PushConfigStockItemVo> newList = pushConfigStockDto.getNewPushConfigItemList();
        if(pushConfigStockVo == null){
            //添加数据
            pushConfigStockDto1.setPushConfigId(PushConfigTypeEnum.VENDING_MACHINE_INVENTORY.getCode());
            pushConfigStockDao.insert(pushConfigStockDto1);
        }else{
            //修改库存
            pushConfigStockDto1.setId(pushConfigStockVo.getId());
            pushConfigStockDao.update(pushConfigStockDto1);
        }

        // 求旧集合和新集合交集 修改操作
//            List<PushConfigStockItemVo> updateList = newList.stream().filter(item ->
//                    oldList.stream().map(PushConfigStockItemVo::getPcsiId).collect(Collectors.toList()).contains(item.getPcsiId())
//            ).collect(Collectors.toList());
//            updateList.forEach(item -> {
//                pushConfigStockItemDao.update(item);
//            });

        // 求新集合差集 添加操作
        Long id;
        if(pushConfigStockVo != null){
            id = pushConfigStockVo.getId();
        } else {
            id = pushConfigStockDto1.getId();
        }
        List<PushConfigStockItemVo> addList = newList.stream().filter(item ->
                !oldList.stream().map(PushConfigStockItemVo::getPcsiId).collect(Collectors.toList()).contains(item.getPcsiId())
        ).collect(Collectors.toList());
        addList.forEach(item -> {
            item.setPushConfigStockId(id);
            pushConfigStockItemDao.insert(item);
        });

        // 求旧集合差集 删除操作
        List<PushConfigStockItemVo> delList = oldList.stream().filter(item ->
                !newList.stream().map(PushConfigStockItemVo::getPcsiId).collect(Collectors.toList()).contains(item.getPcsiId())
        ).collect(Collectors.toList());
        if(!StringUtils.isEmpty(delList) && delList.size() > 0){
            pushConfigStockItemDao.deleteBatch(delList);
        }
        return ServerResponse.createBySuccess();
    }
}
